﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cloud.Boss.Models;
using Cloud.Core.Cache;
using Cloud.Framework.Filter;
using Cloud.Framework.Model;
using Cloud.Service.Log;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Cloud.Boss.Controllers
{
    public class MainController : BaseController
    {
        private readonly ICacheManager _cacheManager;
        public MainController(IOptions<ProjectSetting> _projectSetting, ILogService _logService, ICacheManager _cacheManager) : base(_projectSetting, _logService)
        {
            this._cacheManager = _cacheManager;
        }
        [LoginAction]
        [NoLogAction]

        public IActionResult Index(string nodeid=null)
        {
            ViewBag.nodeid = nodeid;
            ViewBag.siteName = _projectSetting.Value.SystemName;
            var currentSysUser = CurrentSysUser;
            ViewBag.name = currentSysUser.LoginName;
            var permissions = _cacheManager.Get<List<CurrentSysUserPermission>>(currentSysUser.PermissionCacheKey);
            var firstMenus = permissions.Where(c => c.parentId == 0).ToList();
            ViewBag.firstMenus = firstMenus;
            return View();
        }
        [LoginAction]
        [NoLogAction]
        public ActionResult LeftPermissionMenuView(int parentId)
        {

            var currentSysUser = CurrentSysUser;
            var permissions = _cacheManager.Get<List<CurrentSysUserPermission>>(currentSysUser.PermissionCacheKey);
            var twoMenus = permissions.Where(c => c.parentId == parentId).ToList();
            List<VM_Persimmion_Menu_Combined> permissionMenus = new List<VM_Persimmion_Menu_Combined>();
            foreach (var item in twoMenus)
            {
                VM_Persimmion_Menu_Combined twoMenu = new VM_Persimmion_Menu_Combined();
                twoMenu.id = item.id;
                twoMenu.name = item.name;
                twoMenu.menus = new List<VM_Persimmion_Menu>();
                var threeMenus = permissions.Where(c => c.parentId == item.id).ToList();
                foreach (var three in threeMenus)
                {
                    VM_Persimmion_Menu menu = new VM_Persimmion_Menu()
                    {
                        id = three.id,
                        name = three.name,
                        url = three.url
                    };
                    twoMenu.menus.Add(menu);
                }
                permissionMenus.Add(twoMenu);
            }
            ViewBag.parentId = parentId;
            return View(permissionMenus);

        }
        [LoginAction]
        [NoLogAction]
        public IActionResult Panel()
        {
            return View();
        }

      
    }
}